<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="项目" prop="grantId">
        <el-select v-model="queryParams.grantId" placeholder="请选择项目" @change="grantSelectChange">
          <el-option
            v-for="dict in grantOptions"
            :key="dict.grantId"
            :label="dict.grantName"
            :value="dict.grantId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['grant:familiesexampoint:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grant:familiesexampoint:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grant:familiesexampoint:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['grant:familiesexampoint:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="familiesexampointList" @selection-change="handleFamiliesSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目" align="center" prop="grantName" />
      <el-table-column label="考点编号" align="center" prop="epCode" />
      <el-table-column label="考点名称" align="center" prop="epName" />
      <el-table-column label="科类" align="center" prop="familiesName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['grant:familiesexampoint:edit']"
          >设置科类</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改科类考点关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1024px" append-to-body>
      <el-form :model="familiesQueryParams" ref="familiesQueryForm" :inline="true" label-width="68px">
<!--        <el-form-item label="大类" prop="grantId">-->
<!--          <el-select v-model="familiesQueryParams.parentId" placeholder="请选择科类" @change="familiesTypeSelectChange">-->
<!--            <el-option-->
<!--              v-for="dict in familiesTypeOptions"-->
<!--              :key="dict.id"-->
<!--              :label="dict.familiesName"-->
<!--              :value="dict.id"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="显示已选" prop="allFlag">
          <el-select v-model="familiesQueryParams.allFlag" placeholder="显示其它考点已选">
            <el-option
              v-for="dict in yesnoOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="大类" prop="allFlag">
          <el-select v-model="familiesQueryParams.parentId" clearable placeholder="请选择大类">
            <el-option
              v-for="dict in familiesTypeOptions"
              :key="dict.id"
              :label="dict.familiesName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleFamiliesQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="familiesSelectList" ref="familiesSelectTable" @selection-change="familiesSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="大类" align="center" prop="parentFamiliesName" />
        <el-table-column label="科类代码" align="center" prop="familiesCode" />
        <el-table-column label="科类" align="center" prop="familiesName" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFamiliesexampoint, getFamiliesexampoint, delFamiliesexampoint, addFamiliesexampoint, updateFamiliesexampoint, exportFamiliesexampoint,getFamiliesSelectList } from "@/api/grant/familiesexampoint";
import { listGrantfamilies } from "@/api/grant/grantfamilies";
import {listGrantAll} from "@/api/grant/grant";
export default {
  name: "Familiesexampoint",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 科类考点关联表格数据
      familiesexampointList: [],
      familiesSelectList:[],
      //科类大类下拉选择
      familiesTypeOptions:[],
      yesnoOptions: [],
      grantOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        grantId: undefined,
        granttypeId: undefined
      },
      familiesQueryParams:{
        parentId: undefined,
        granttypeId:undefined,
        allFlag: 'N',
        grantId: undefined,
        examPointId: undefined,
      },
      // 表单参数
      form: {
        parentId: undefined,
        granttypeId: undefined,
        grantId: undefined,
        examPointId: undefined,
        familiesIds: undefined
      },
      familiesQueryForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("sys_yes_no").then(response => {
      this.yesnoOptions = response.data;
    });
    let query ={};
    query["isEnabled"]="0";
    listGrantAll(query).then(res=>{
      this.grantOptions = res.data;
      this.queryParams.grantId=res.data[res.data.length-1].grantId;
      this.queryParams.granttypeId=res.data[res.data.length-1].granttypeId;
      this.getList();
    })
  },
  methods: {
    //科类选择弹窗获取科类大类列表
    getfamiliesTypeOptions(){
      let query= {};
      query["granttypeId"] = this.queryParams.granttypeId;
      query["parentId"] = 0;
      listGrantfamilies(query).then(response=>{
        this.familiesTypeOptions = response.data;
        //if(this.familiesTypeOptions.length>0){
        //  this.familiesQueryParams.parentId = this.familiesTypeOptions[0].id;
        //  this.handleFamiliesQuery();
        //}
        this.handleFamiliesQuery();

      })
    },
    /** 查询科类考点关联列表 */
    getList() {
      this.loading = true;
      listFamiliesexampoint(this.queryParams).then(response => {
        this.familiesexampointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examPointId: undefined,
        familiesId: undefined,
        grantId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.examPointId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    familiesSelectionChange(selection){
      this.form.familiesIds = selection.map(item => item.familiesId);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加科类考点关联";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.familiesQueryParams.parentId = undefined;
      this.familiesQueryParams.examPointId = row.examPointId;
      this.form.examPointId = row.examPointId;
      this.form.grantId = row.grantId;
      this.form.parentId = this.familiesQueryParams.parentId;
      this.familiesQueryParams.granttypeId = row.granttypeId;
      this.getfamiliesTypeOptions();
      this.open = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      addFamiliesexampoint(this.form).then(res =>{
        if(res.code === 200){
          this.msgSuccess("保存成功");
          this.open=false;
          this.getList();
        }
      })
      console.log(this.form);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examPointIds = row.examPointId || this.ids;
      this.$confirm('是否确认删除科类考点关联编号为"' + examPointIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFamiliesexampoint(examPointIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有科类考点关联数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFamiliesexampoint(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },grantSelectChange(value){
      Object.keys(this.grantOptions).map((key)=>{
        if(this.grantOptions[key].grantId==value){
          this.queryParams.granttypeId = this.grantOptions[key].granttypeId
          return;
        }
      })

    },familiesTypeSelectChange(){
      this.handleFamiliesQuery();
    },handleFamiliesQuery(){
      this.familiesQueryParams.grantId = this.queryParams.grantId;
      //this.familiesQueryParams.parentId = undefined;
      getFamiliesSelectList(this.familiesQueryParams).then(response=>{
        this.familiesSelectList = response.data;
        this.familiesSelectList.forEach((item,index)=>{
          if(item.selectFlag==='1'){
            this.$nextTick(()=>{
              this.$refs.familiesSelectTable.toggleRowSelection(this.familiesSelectList[index], true)
            })
          }
        })
      })
    },handleFamiliesSelectionChange(selections){

    }
  }
};
</script>
